﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using System.Data;
using System.Web.Routing;
using VQWEB.common;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for ProductHandler
    /// </summary>
    public class core_ProductHandler : IHttpHandler, IRouteHandler
    {
      
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Product
            string add_product = context.Request["add_product"];
            if (!string.IsNullOrEmpty(add_product))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_product>(add_product);
                if (obj.tag_array_id != null)
                    bussiness.InsertAllInOne<core_tag_product>(obj.tag_array_id, "tag_id", "product_id", obj.product_id);
                if (obj.category_array_id != null)
                    bussiness.InsertAllInOne<core_category_product>(obj.category_array_id, "category_id", "product_id", obj.product_id);
                context.Response.Write("Thực thi thành công !");
            }

            string view_product = context.Request["view_product"];
            if (!string.IsNullOrEmpty(view_product))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderCategoryTag(context);
            }
            string list_product = context.Request["list_product"];
            if (!string.IsNullOrEmpty(list_product))
            {
                RenderList(context);
            }
            string delete_product = context.Request["delete_product"];
            if (!string.IsNullOrEmpty(delete_product))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_product>(delete_product);
                RenderList(context);
            }
            
            string lockunlock_product = context.Request["lockunlock_product"];
            if (!string.IsNullOrEmpty(lockunlock_product))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_product>(lockunlock_product, "product_active");
                RenderList(context);
            }

           
            #endregion
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            object obj = bussiness.Get<core_product>(
               c => c.product_id.Equals(Id),
               c => new
               {
                   id = c.product_id,
                   title = c.product_title,
                   description = c.product_description,
                   content = c.product_content,
                   image = c.product_img,
                   price=c.product_price,
                   discounts=c.product_discounts,
                   hit = c.product_hit,
                   date = c.product_date,
                   active = c.product_active
               });
            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_category_id = bussiness.List<core_category_product>(
                null,
                null,
                n => n.product_id.Equals(value),
                null,
                n => n.category_id
                ).ToList();
            List<object> array_tag_id = bussiness.List<core_tag_product>(
               null,
               null,
              null,
               null,
               n => n.tag_id
               ).ToList();
            List<Dictionary<string, object>> listcategory = bussiness.RenderArrayCheckboxJSON(CoreData.ViewCategory("product"), array_category_id, "id", "ten", "category_array_id[]");
            List<Dictionary<string, object>> listtag = bussiness.RenderArrayCheckboxJSON(CoreData.ViewTag(), array_tag_id, "id", "keyword", "tag_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("product", obj);
            dic.Add("category", listcategory);
            dic.Add("tag", listtag);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {

            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            IList<object> list = bussiness.List<core_product>(
                null,
                null,
                null,
                null,
              c => new
              {
                  id = c.product_id,
                  title = c.product_title,
                  description = c.product_description,
                  content = c.product_content,
                  image = c.product_img,
                  price = c.product_price,
                  discounts = c.product_discounts,
                  hit = c.product_hit,
                  date = c.product_date,
                  active = c.product_active
              });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        private void RenderCategoryTag(HttpContext context)
        {
            BusinessLayer<CoreModelDataContext> business = new BusinessLayer<CoreModelDataContext>();
            var category = business.RenderArrayCheckboxJSON(CoreData.ViewCategory("product"), null, "id", "ten", "category_array_id[]");
            var tag = business.RenderArrayCheckboxJSON(CoreData.ViewTag(), null, "id", "keyword", "tag_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("tag", tag);
            dic.Add("category", category);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext RequestContext)
        {
            return this;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}